﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using AGRTraining.za.co.agrtraining.model;
using AGRTraining.za.co.agrtraining.repository.Impl;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
namespace AGRTraining.za.co.agrtraining.services.impl
{
    public class EmployeeServiceImpl:IEmployeeService
    {
        private EmployeeDAO empDao = new EmployeeDAO();
        private UserDAO userDao = new UserDAO();
        private AGRModelContainer agrDb = new AGRModelContainer();
        public Employee getEmployeeDetails(string employeeCode) 
        {
            Employee employee = new Employee();
            employee = empDao.findById(employeeCode);
            return employee;
        }
        public IEnumerable<Employee> viewAllEmployees() 
        {
            IEnumerable<Employee> employees = new List<Employee>();
            employees = empDao.findAll();
            return employees;
        }
        public IEnumerable<Employee> viewAllTrainers() {
            var trains = from t in agrDb.Employees
                           where t.IsTrainer == 1//"true" it is a trainer
                           select t;
            IEnumerable<Employee> trainers = new List<Employee>();
            trainers = trains;
            return trainers;
        }
        public IEnumerable<Qualification> viewEmployeeQualifications(string empCode)
        {
            var quals = from emp in agrDb.Employees
                        from factQual in agrDb.FactTrainerQualifications
                        from qual in agrDb.Qualifications
                        where emp.EmployeeCode == empCode
                        where emp.EmployeeCode == factQual.EmployeeCode
                        where factQual.QualificationCode == qual.QualificationCode
                        select qual;
            IEnumerable<Qualification> qualifications = new List<Qualification>();
            qualifications = quals;
            return qualifications;
        }
        public void updateEmployeePassword(string userName, string password)
        {
            User user = new User();
            user.UserName = userName;
            user.UserPassword = password;
            userDao.merge(user);
        }
        public IEnumerable<Employee> viewAllTrainersPerCourse(string courseCode)
        {
            var trains = from crs in agrDb.Courses
                         from factCrsSub in agrDb.FactCourseSubjectTrainers
                         from emp in agrDb.Employees
                         where crs.CourseCode == courseCode
                         where crs.CourseCode == factCrsSub.CourseCode
                         where factCrsSub.EmployeeCode == emp.EmployeeCode
                         select emp;
            return trains;
        }
    }
}